#use "calc_parser.ml";;

let input = read_line ();;

let p = ref (Parser.create ());;
let length = String.length input;;
for i = 0 to length - 1 do
	p := Parser.post (i + 1) input.[i] !p
done;;
let p = Parser.post (length + 1) '\x1a' !p;;

let rec eval (e: int AST.expr option): int = (
	begin match e with
	| Some (AST.Num (_, n)) -> eval_num 0 n
	| Some (AST.Add (x, _, y)) -> eval x + eval y
	| Some (AST.Sub (x, _, y)) -> eval x - eval y
	| Some (AST.Mul (x, _, y)) -> eval x * eval y
	| Some (AST.Div (x, _, y)) -> eval x / eval y
	| None -> assert false
	end
) and eval_num (v: int) (e: int AST.num option): int = (
	begin match e with
	| Some (AST.Digit (Some d)) -> v * 10 + Char.code d - Char.code '0'
	| Some (AST.Digits (Some d, ds)) -> eval_num (v * 10 + Char.code d - Char.code '0') ds
	| _ -> assert false
	end
);;

let rec print_error ((p, e): int * Parser.error) = (
	print_int p;
	print_string ":";
	Parser.pp_error (fun f c -> Format.fprintf f "\"%c\"" c) Format.std_formatter e;
	Format.print_flush ();
	print_newline ()
);;

List.iter print_error (List.rev (Parser.errors p));;
if Parser.accepted p then (
	if Parser.errors p = [] then (
		print_int (eval (Parser.result p));
		print_newline ()
	)
) else (
	raise (Failure "NOT ACCEPTED!!")
);;

